package utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class UtilTools {
	/***
	 * 下划线命名转为驼峰命名
	 * 
	 * @param para
	 *            下划线命名的字符串
	 */
	/**规则条件字母****/
	public static final String ALPHABET ="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	
	public static String UnderlineToHump(String para) {
		StringBuilder result = new StringBuilder();
		String a[] = para.split("_");
		for (String s : a) {
			if (result.length() == 0) {
				result.append(s.toLowerCase());
			} else {
				result.append(s.substring(0, 1).toUpperCase());
				result.append(s.substring(1).toLowerCase());
			}
		}
		return result.toString();
	}

	/***
	 * 驼峰命名转为下划线命名
	 * 
	 * @param para
	 *            驼峰命名的字符串
	 */

	public static String HumpToUnderline(String para) {
		StringBuilder sb = new StringBuilder(para);
		int temp = 0;// 定位
		for (int i = 0; i < para.length(); i++) {
			if (Character.isUpperCase(para.charAt(i))) {
				sb.insert(i + temp, "_");
				temp += 1;
			}
		}
		return sb.toString().toUpperCase();
	}

	/*
	 * (特殊字符替换)
	 *
	 * 
	 * @return String 返回类型
	 * 
	 * @author xsw
	 * 
	 * @2016-12-4下午03:10:03
	 */
	public static String htmlReplace(String str) {
		str = str.replace("&nbsp;", " ");
		str = str.replace("&amp;nbsp;", " ");
		str = str.replace("&lt;", "<");
		str = str.replace("&gt;", ">");
		str = str.replace("&lt;&gt;", "<>");
		return str;
	}

	/**
	 * 时间切片处理
	 * 
	 * @param number
	 *            数字
	 * @param unit
	 *            单位
	 * @return
	 */
	public static String timeSlice(String number, String unit) {
		String dateSliceCondition = "";
		// 先将时间切片过滤条件自动生成
		if (unit.equals("M")) {
			// 按月切，比较特殊
			dateSliceCondition = "between regexp_replace(add_months(current_date,-" + number + "), '-', '') and";
		} else if (unit.equals("W")) {
			// 按周切，类似天，但是是7倍
			dateSliceCondition = "between regexp_replace(date_sub(current_date," + number + " * 7}), '-', '') and";
		} else {
			// 按天切
			dateSliceCondition = "between regexp_replace(date_sub(current_date," + number + "), '-', '') and";
		}
		return dateSliceCondition;
	}
	
	/**
	 * 时间中文切片处理
	 * 
	 * @param number
	 *            数字
	 * @param unit
	 *            单位
	 * @return
	 */
	public static String timeSliceCN(String number, String unit) {
		String dateSliceCondition = "";
		// 先将时间切片过滤条件自动生成
		if (unit.equals("M")) {
			// 按月切，比较特殊
			dateSliceCondition = "过去"+number+"月";
		} else if (unit.equals("W")) {
			// 按周切，类似天，但是是7倍
			dateSliceCondition = "过去"+number+"周";
		} else {
			// 按天切
			dateSliceCondition = "过去"+number+"天";
		}
		return dateSliceCondition;
	}
	  //截取数字 
	  public static String getNumbers(String content) { 
	    Pattern pattern = Pattern.compile("\\d+"); 
	    Matcher matcher = pattern.matcher(content); 
	    while (matcher.find()) { 
	      return matcher.group(0); 
	    } 
	    return ""; 
	  } 
	  
	// 截取非数字 
	public static String splitNotNumber(String content) { 
	  Pattern pattern = Pattern.compile("\\D+"); 
	  Matcher matcher = pattern.matcher(content); 
	  while (matcher.find()) { 
	    return matcher.group(0); 
	  } 
	  return ""; 
	} 
	
	/**
	 * 验证是否包含数字
	 * @param passWorld
	 * @return
	 */
	public static boolean hasNumber(String passWorld){
		String regex = "[0-9]";
		return match(regex,passWorld);
		
	}
	
	/**
	 * 验证是否是数字
	 * @param passWorld
	 * @return
	 */
	public static boolean isNumber(String str){
		String regex = "^[-.0-9]*$";
		return  Pattern.matches(regex, str);
	}
	
	/**
	 * 验证是否包含小写字母
	 * @param passWorld
	 * @return
	 */
	public static boolean hasLowercase(String passWorld){
		String regex = "[a-z]";
		return match(regex,passWorld);
		
	}
	
	/**
	 * 验证是否包含大写字母
	 * @param passWorld
	 * @return
	 */
	public static boolean hasUppercase(String passWorld){
		String regex = "[A-Z]";
		return match(regex,passWorld);
		
	}
	
	/**
	 * 验证是否包含特殊字符
	 * @param passWorld
	 * @return
	 */
	public static boolean hasSpecialCharacter(String passWorld){
		String regex = "[-_.!@#$%~^&*()+=?><]";
		return match(regex,passWorld);
		
	}
	
	/**
	 * 是否正则匹配
	 * @param regex
	 * @param str
	 * @return
	 */
	public static boolean match(String regex,String str){
		Pattern p = Pattern.compile(regex);
		Matcher m = p.matcher(str);
		return m.find();
	}
	
	/**
	 * 
	 * @param str
	 * @return
	 */
     public static boolean checkPassWord(String passWorld){
    	 
    	 if(passWorld.length() < 8 && passWorld.length() > 20){
    		 return false;
    	 }
    	  int raceCount = 0;   // 种类统计
	      if (hasNumber(passWorld)) {
	        raceCount++;
	      }
	      if (hasLowercase(passWorld)) {
	        raceCount++;
	      }
	      if (hasUppercase(passWorld)) {
	        raceCount++;
	      }
	      if (hasSpecialCharacter(passWorld)) {
	        raceCount++;
	      }
	      if (raceCount >= 3) {
	    	  return true;
	      }
    	 return false;
     }
     
     public static int getAlphabetPostion(String s){
    	 return ALPHABET.indexOf(s);
     }
     
 	public static  boolean isExcludedURL(String url) {
		if ((url.toLowerCase().contains("save")) || (url.toLowerCase().contains("update")) ||  (url.toLowerCase().contains("delete"))){
			return true;
		}
         return false;
	}

}
